<template>
  <div class="addCarType">

    <el-dialog width="600px" :visible="true" :before-close="cancelBtn" :close-on-click-modal="false">
      <div slot="title">
        <div class="addTitle">{{ title }}</div>
      </div>
      <el-form :model="form" label-position="top" :label-width="formLabelWidth" v-if="state!='info'">
        <el-form-item label="图标:" >
          <ImageUpload v-model="form.icon"/>
        </el-form-item>
        <el-form-item label="名称:" >
          <el-input v-model="form.name" autocomplete="off" placeholder="请输入名称"></el-input>
        </el-form-item>
        <el-form-item label="类型:" >
          <el-radio-group v-model="form.type">
            <el-radio :label="'link'">链接</el-radio>
            <el-radio :label="'mini'">小程序</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="小程序appid:" v-if="form.type=='mini'">
          <el-input v-model="form.appid" autocomplete="off" placeholder="请输入小程序appid"></el-input>
        </el-form-item>
        <el-form-item label="路径:" v-if="form.type=='link'">
          <el-input v-model="form.url" autocomplete="off" placeholder="请输入url"></el-input>
        </el-form-item>


      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelBtn">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {settingSet,settingInfo} from "@/api"
import EditorBar from "@/components/wangEditor";
export default {
  name: "addClass",
  components: {
    EditorBar
  },
  props:['item','state','closeAlert'],
  data(){
    return{
      form: {
        name:'',
        type:'link',
        icon:'',
        appid:'',
        url:'',
      },
      formLabelWidth: '120px',
      title: '',
      list:[]
    }
  },
  mounted() {
    console.log(this.state)
    this.getList()

  },
  methods:{
    getList(){
      settingInfo({keys:'commonTools'}).then(res=>{
        if(res.code==10000){
          this.list =JSON.parse(res.data.commonTools)
          if(this.state == 'edit'){
            this.info()
            this.title = '编辑'
          }else{
            this.title = '新增'
          }
        }
      })
    },
    // 详情
    async info(){
      console.log(this.list,this.item.$index)
      this.form = this.list[this.item.$index]

      console.log(this.form)
    },
    // 确认
    async confirm(){
      if(this.state == 'add'){
        // 添加
        this.handleConvenientSave();
      }else{
        // 修改
        this.handleConvenientEdit()
      }

    },
    async handleConvenientSave(){
      console.log(this.list,this.form)
      this.list.push(this.form)
      console.log(this.list)
      settingSet({
        key:'commonTools',
        value:JSON.stringify(this.list),
        value_type:'json',
        is_active:true
      }).then(res=>{
        console.log(res)
        if (res.code==10000){
          this.$message({
            message: res.message,
            type: 'success'
          });
          this.cancelBtn();
        }
      })
      // return
      // await integratedServiceAdd(this.form).then(res=>{
      //   if (res.code==10000){
      //     this.$message({
      //       message: res.message,
      //       type: 'success'
      //     });
      //     this.cancelBtn();
      //   }
      // })
    },
    async handleConvenientEdit(){
      console.log(this.list)
      settingSet({
        key:'commonTools',
        value:JSON.stringify(this.list),
        value_type:'json',
        is_active:true
      }).then(res=>{
        console.log(res)
        if (res.code==10000){
          this.$message({
            message: res.message,
            type: 'success'
          });
          this.cancelBtn();
        }
      })
      // await integratedServiceEdit(this.form).then(res=>{
      //   if (res.code==10000){
      //     this.$message({
      //       message: res.message,
      //       type: 'success'
      //     });
      //     this.cancelBtn();
      //   }
      // })
    },
    // 关闭弹窗
    cancelBtn(){
      this.$emit('closeAlert',false)
    }
  }
}
</script>

<style scoped lang="less">
.addTitle{
  font-size: 28px;
  font-family: PingFangSC-Semibold, PingFang SC;
  font-weight: 600;
  color: #000000;
}
/deep/.el-dialog__header,
/deep/.el-dialog__body,
/deep/.el-dialog__footer{
  padding-left: 45px;
  padding-right: 45px;
}
/deep/.el-form-item__label{
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #000000;
}
///deep/.el-form-item__content input{
//  border: none;
//  background: #F4F4F4;
//  border-radius: 10px;
//}
.el-select{
  width: 100%;
}

</style>

